<?php
/**
 * Created by PhpStorm.
 * User: bob-chou
 * Date: 17/2/12
 * Time: 20:15
 */
namespace Common\Model;

use Think\Model;

/**
 * 文章附属表
 * Class NewsContentModel
 * @package Common\Model
 */
class NewsContentModel extends  Model {
    /**
     * @var \Model|Model
     */
    private  $_db = '';

    /**
     * NewsContentModel constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->_db = M('news_content');
    }

    /**
     * 文章内容插入文章附表
     * @param $data
     * @return int
     */
    public function insert($data)
    {
        if (!$data || !is_array($data)){
            return 0;
        }
        $data['create_time'] = time();
        /*编辑器内的字符转义 */
        if ($data['content'] && isset($data['content'])){
            $data['content'] = htmlspecialchars($data['content']);
        }
        return $this->_db->add($data);
    }

    /**
     * 更新文章附表内容
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateNewsById($id , $data)
    {
        if (!$id || !is_numeric($id)){
            throw_exception("id不合法");
        }
        if (!$data || !is_array($data)){
            throw_exception("更新的附表内容不和法");
        }
        $res = $this->_db
            ->where("news_id = " . $id)
            ->save($data);
        return $res;
    }
}